// pages/ucenter/income/income.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();


Page({

  /**
   * 页面的初始数据
   * indexList 活动列表
   */
  data: {
    pagination: {
      page: 1,
      totalPage: 0,
    },
    adList: [],
    blogList: [],
    loadMore: true
  }, // 获取轮播
  getAdList: function() {
    let that = this;
    util.request(api.AdList).then(function(res) {
      if (res.code === 0) {
        that.setData({
          adList: res.data
        });
      }
    });
  },
  // 获取首页活动列表
  getBlogList: function(page) {
    let that = this;
    util.request(api.BlogList, {
      page: page,
      ptypeId: 100,
      isIndex: 1
    }).then(function(res) {
      wx.hideLoading();
      if (res.code == 0) {
        let _items = that.data.blogList.concat(res.dataList);
        that.setData({
          blogList: _items
        });
        if (page >= res.pagination.totalPage) {
          that.setData({
            loadMore: false
          })
        }
      }
    })
  },
  //事件处理函数
  bindItemTap: function(e) {
    let blogId = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/detail/detail?blogId=' + blogId
    })
  },
  // 加载更多
  fetchOrderList: function() {
    let that = this,
      loadMore = that.data.loadMore,
      pagination = this.data.pagination;
    pagination.page = pagination.page + 1;
    console.log('%s---%s----%s', pagination.page,loadMore, pagination.totalPage)
    this.setData({
      pagination: pagination
    });
    if (loadMore) {
      this.getBlogList(pagination.page);
    } else {
      this.setData({
        loadMore: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    this.getAdList();
    this.getBlogList(1);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.fetchOrderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 图片加载失败给到默认图片
   */
  errorloadImage: function(e) {
    console.log('eee')
    if (e.type == "error") {
      var index = e.target.dataset.index
      var blogList = this.data.blogList
      blogList[index].imgUrl = app.globalData.defaultImageUrl
      this.setData({
        blogList: blogList
      })
    }
  }
})